<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Model;

/**
 * @property int $id ID
 * @property int $im_id 聊天ID
 * @property string $username 用户名
 * @property string $nickname 昵称
 * @property string $mobile 手机号码
 * @property string $country_code 手机区号
 * @property string $email 邮箱
 * @property int $role_id 角色 1-普通用户 2-主播 3-专家 4-助理 9-游客
 * @property string $password 密码
 * @property string $salt 盐
 * @property int $coins 金币
 * @property int $integral 积分
 * @property int $is_realname 是否实名
 * @property int $is_virtual 虚拟用户
 * @property string $device_id 游客设备ID
 * @property int $reg_time 注册时间
 * @property string $reg_ip 注册IP
 * @property int $reg_client 客户端
 * @property int $last_time 最后登录时间
 * @property string $last_ip 最后登录IP
 * @property-read mixed $userid 
 * @property-read null|UserModelAnchor $anchor 
 * @property-read null|Live $live 
 * @property-read string $avatar 头像
 */
class User extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'user';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'im_id', 'username', 'nickname', 'mobile', 'country_code', 'email', 'avatar', 'role_id', 'password', 'salt', 'coins', 'integral', 'is_realname', 'is_virtual', 'device_id', 'reg_time', 'reg_ip', 'reg_client', 'last_time', 'last_ip'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'im_id' => 'integer', 'role_id' => 'integer', 'coins' => 'integer', 'integral' => 'integer', 'is_realname' => 'integer', 'is_virtual' => 'integer', 'reg_time' => 'integer', 'reg_client' => 'integer', 'last_time' => 'integer'];

    public function getUseridAttribute()
    {
        return $this->id;
    }

    /**
     * 头像处理
     * @param $value
     * @return string
     */
    public function getAvatarAttribute($value): string
    {
        return img_url($value);
    }

    /**
     * 检查字段值是否存在.
     * @param mixed $field
     * @param mixed $value
     */
    public static function isExists($field, $value): bool
    {
        return self::query()->where($field, $value)->exists();
    }

    public function anchor()
    {
        return $this->hasOne(UserModelAnchor::class, 'userid', 'id');
    }

    public function live(){
        return $this->hasOne(Live::class, 'userid', 'id');
    }
}
